package com.warrior.games.activitys;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.androidWarrior.R;
import com.warrior.games.Game;
import com.warrior.games.Game.GAME_STATES;
import com.warrior.games.Game.IStateGame;
import com.warrior.games.LaunchingGame.ILaunchingGameMode;
import com.warrior.games.LaunchingGame.LAUNCHING_GAME_MODE;
import com.warrior.games.MusicGame;
import com.warrior.games.activitys.MenuGamePopup.IClickMenuGame;
import com.warrior.games.activitys.MenuGamePopup.TYPE_CLICK;
import com.warrior.main.MyActivity;
import com.warrior.main.MyApp;
import com.warrior.main.MyApp.APP_STATES;
import com.warrior.main.MyFacebook;

public abstract class GameActivity extends MyActivity 
	implements  IStateGame,IClickMenuGame,ILaunchingGameMode{

	protected Game game;
	private MenuGamePopup menuPopup;
	private MusicGame music;
	private TextView tvLaunchingGameMode,tvMyName,tvCompetitorName;
	private View container;
	protected SeekBar mySbStatus,competitorSbStatus;
	private MyApp app;
	private static int RED;
	private static int GREEN;
	private static int YELLOW;
	private static int BLACK;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		menuPopup = new MenuGamePopup(this);
		menuPopup.setOnClickButton(this);
		
		app = (MyApp)getApplication();
		buildViewReference();
		buildViewEvents();
		buildGame();
		
		RED = getResources().getColor(R.color.red);
		GREEN = getResources().getColor(R.color.green);
		YELLOW = getResources().getColor(R.color.yellow);
		BLACK = getResources().getColor(R.color.black);
		resetGameActivity();
	}
	protected void buildViewReference(){
		container = findViewById(R.id.container);
		tvLaunchingGameMode = (TextView)findViewById(R.id.tvLaunchingGameMode);
		tvMyName = (TextView)findViewById(R.id.tvMyName);
		tvCompetitorName = (TextView)findViewById(R.id.tvCompetitorName);
		
		tvMyName.setText(app.getFacebook().getMyUserName() + " " + app.getMyDeviceName());
		tvCompetitorName.setText(app.getFacebook().getCompetitorUserName() + " " + app.getCompetitorDeviceName());
		buildSeekBars();
	}
	protected void buildViewEvents(){
		
	}
	private void buildSeekBars(){
		
		mySbStatus = (SeekBar)findViewById(R.id.mySbStatus);
		competitorSbStatus= (SeekBar)findViewById(R.id.competitorSbStatus);
		// because is not allow in layout_game.xml file
		mySbStatus.setEnabled(false);
		competitorSbStatus.setEnabled(false);
		
		MyFacebook f = app.getFacebook();
		if(f.isOpenedConnection() && f.getMyProfilePictureView() != null){
			Drawable myDrawable = MyFacebook.convertToDrawable(app, f.getMyProfilePictureView());
			if(myDrawable != null){	
				mySbStatus.setThumb(myDrawable);
			}
		}
		Drawable competitorDrawable = MyFacebook.convertToDrawable(app, f.getCompetitorProfilePictureView());
		if(competitorDrawable != null){
			competitorSbStatus.setThumb(competitorDrawable);
		}
	}
	protected void onPause() {
		if(game.getState() != GAME_STATES.FINISHED){
			game.setPause(true);
		}
		super.onPause();
	}
	protected void onResume() {
		if(game.isPause()){
			showMenuPopup();
		}
		super.onResume();
	}
	private void showMenuPopup(){
		if(!menuPopup.isShowing()){
			menuPopup.show();
		}
		switch(game.getState()){
			case NOT_YET:{
				menuPopup.setGoneButton(R.id.butRematch);
				menuPopup.setGoneButton(R.id.butUploadPost);
				break;
			}
			case RUNNING:{
				menuPopup.setGoneButton(R.id.butRematch);
				menuPopup.setGoneButton(R.id.butUploadPost);
				break;
			}
			case FINISHED:{
				if(!menuPopup.isShowing()){
					menuPopup.show();
				}
				menuPopup.setGoneButton(R.id.butResume);
				break;
			}
		}
	}
	protected void buildGame(){
		game.setStateGameListener(this);
		game.getLaunchingGame().setLaunchingModeChangedListener(this);
		music = new MusicGame(this);
	}
	public void readyGame(){
		
	}
	public void startGame() {

	}
	public void pauseGame() {
		music.pause();
	}
	public void resumeGame() {
		if(menuPopup.isShowing()){
			menuPopup.dismiss();
		}
		music.resume();
	}
	protected void showWinningAfterFinish(boolean isWinner){
		if(isWinner){
			container.setBackgroundColor(GREEN);
			mySbStatus.setProgress(mySbStatus.getMax());
		}
		else{
			container.setBackgroundColor(RED);
			competitorSbStatus.setProgress(mySbStatus.getMax());
		}
	}
	public void myStatusGame(int myStatus) {
		int competitorStatus = competitorSbStatus.getProgress();
		mySbStatus.setProgress(myStatus);
		showWinningOnline(myStatus, competitorStatus);
	}
	public void competitorStatusGame(int competitorStatus) {
		int myStatus = mySbStatus.getProgress();
		competitorSbStatus.setProgress(competitorStatus);
		showWinningOnline(myStatus, competitorStatus);
	}
	public void rematchGame() {
		resetGameActivity();
	}
	protected void resetGameActivity(){
		mySbStatus.setProgress(0);
		competitorSbStatus.setProgress(0);;
		container.setBackgroundColor(BLACK);
		if(app.isServer()){
			game.setReady(true);
		}
	}
	public void finishGame(boolean isWinner) {
		showWinningAfterFinish(isWinner);
		if(isWinner){
			showMenuPopup();
		}
	}
	public void closeGame(){
		app.setState(APP_STATES.SELECT_GAME);
		music.stop();
		setResult(RESULT_OK);
		this.finish();
	}
	public void onBackPressed() {
		if(game.getState() != GAME_STATES.FINISHED){
			if(!game.isPause()){
				game.setPause(true);
				showMenuPopup();
			}
		}
	}
	public void selectOption(TYPE_CLICK type) {
		switch(type){
			case uploadPost:{
				if(game.getState() == GAME_STATES.FINISHED){
					MyFacebook fb = app.getFacebook();
					if(fb.isOpenedConnection()){
						//String text = fb.getUser().getName() + ", you're a big shot!! you won "; 
						//fb.writePost(text ,this);
						//fb.showUploadPostDialog(this);
						facebookOpenSession(true, null);
					}
					else{
						fb.openSession(this);
					}
				}
				break;
			}
			case resume:{
				if(game.getState() != GAME_STATES.FINISHED){
					menuPopup.dismiss();
					game.setPause(false);
				}
				break;
			}
			case rematch:{
				if(game.getState() == GAME_STATES.FINISHED){
					game.setRematch(true);
					menuPopup.dismiss();
				}
				break;
			}
			case close:{
				menuPopup.dismiss();
				game.setClose(true);
				break;
			}
		}
	}
	protected void showWinningOnline(int myStatus,int competitorStatus){
		if(competitorStatus > myStatus){
			container.setBackgroundColor(RED);
			
		}
		else if(competitorStatus < myStatus){
			container.setBackgroundColor(GREEN);
		}
		else{
			container.setBackgroundColor(YELLOW);
		}
	}
	@Override
	public void launchingGameModeChanged(LAUNCHING_GAME_MODE mode) {
		switch (mode) {
			case ready:{
				music.start();
				tvLaunchingGameMode.setTextColor(getResources().getColor(R.color.red));
				tvLaunchingGameMode.setText("ready");
				tvLaunchingGameMode.setVisibility(View.VISIBLE);
				break;
			}
			case set:{
				tvLaunchingGameMode.setText("set");
				tvLaunchingGameMode.setTextColor(getResources().getColor(R.color.yellow));
				break;
			}
			case go:{
				tvLaunchingGameMode.setText("go");
				tvLaunchingGameMode.setTextColor(getResources().getColor(R.color.green));
				game.start();
				break;
			}
			case afterGo:{
				tvLaunchingGameMode.setVisibility(View.GONE);
				break;
			}
		}
	}
}
